package com.zablert.lazyutil.views.color;

import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class ColorPicker extends JPanel {
    public ColorPicker() {
        createUI(this);
    }

    private void createUI(JPanel panel) {
        LayoutManager layout = new FlowLayout();
        panel.setLayout(layout);
        final JLabel colorLabel = new JLabel("颜色选择器示例");
        final JColorChooser colorChooser = new JColorChooser();

        //No preview panel
        colorChooser.setPreviewPanel(new JPanel());
        //Add border
        colorChooser.setBorder(BorderFactory.createTitledBorder("选择标签颜色："));
        AbstractColorChooserPanel[] panels = colorChooser.getChooserPanels();

        //Remove the panels while keeping the last one to show only
        for (int i = 0; i < panels.length - 1; i++) {
            colorChooser.removeChooserPanel(panels[i]);
        }
        colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                Color color = colorChooser.getColor();
                colorLabel.setForeground(color);
            }
        });
        panel.add(colorLabel);
        panel.add(colorChooser);
//        this.add(panel, BorderLayout.CENTER);
    }


}
